|
1
|
|
|
import { Inject } from '@nestjs/common'; |
|
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
|
3
|
|
|
import { |
|
4
|
|
|
Notification, |
|
5
|
|
|
NotificationType |
|
6
|
|
|
} from 'src/Domain/Notification/Notification.entity'; |
|
7
|
|
|
import { CreateNotificationCommand } from './CreateNotificationCommand'; |
|
8
|
|
|
import { INotificationRepository } from 'src/Domain/Notification/Repository/INotificationRepository'; |
|
9
|
|
|
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier'; |
|
10
|
|
|
import { ConfigService } from '@nestjs/config'; |
|
11
|
|
|
|
|
12
|
|
|
const { MATTERMOST_CHANNEL_LEAVES_ID } = process.env; |
|
13
|
|
|
|
|
14
|
|
|
@CommandHandler(CreateNotificationCommand) |
|
15
|
|
|
export class CreateNotificationCommandHandler { |
|
16
|
|
|
constructor( |
|
17
|
|
|
@Inject('INotificationRepository') |
|
18
|
|
|
private readonly notificationRepository: INotificationRepository, |
|
19
|
|
|
@Inject('IMattermostNotifier') |
|
20
|
|
|
private readonly mattermostNotifier: IMattermostNotifier, |
|
21
|
|
|
private readonly configService: ConfigService |
|
22
|
|
|
) {} |
|
23
|
|
|
|
|
24
|
|
|
public async execute(command: CreateNotificationCommand): Promise<string> { |
|
25
|
|
|
try { |
|
26
|
|
|
return await this.createNotification(command); |
|
27
|
|
|
} catch (e) { |
|
28
|
|
|
// On avale l'exception pour éviter d'interrompre les autres traitements. |
|
29
|
|
|
// Tant pis si la notification ne part pas, on finira bien par s'en rendre compte. |
|
30
|
|
|
console.error('Failed to create notification:', e); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
private async createNotification( |
|
34
|
|
|
command: CreateNotificationCommand |
|
35
|
|
|
): Promise<string> { |
|
36
|
|
|
const { message, type, leaveReaquest } = command; |
|
37
|
|
|
|
|
38
|
|
|
if (type === NotificationType.POST) { |
|
39
|
|
|
const { id } = await this.mattermostNotifier.createPost( |
|
40
|
|
|
MATTERMOST_CHANNEL_LEAVES_ID, |
|
41
|
|
|
message |
|
42
|
|
|
); |
|
43
|
|
|
const notification = await this.notificationRepository.save( |
|
44
|
|
|
new Notification(type, message, id, leaveReaquest) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
return notification.getId(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (type === NotificationType.REACTION) { |
|
51
|
|
|
const rootNotification = await this.getRootNotification( |
|
52
|
|
|
leaveReaquest.getId() |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
await this.mattermostNotifier.createReaction( |
|
56
|
|
|
rootNotification.getResourceId(), |
|
57
|
|
|
message |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
const notification = await this.notificationRepository.save( |
|
61
|
|
|
new Notification( |
|
62
|
|
|
type, |
|
63
|
|
|
message, |
|
64
|
|
|
rootNotification.getResourceId(), |
|
65
|
|
|
leaveReaquest |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
return notification.getId(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (type === NotificationType.COMMENT) { |
|
73
|
|
|
const rootNotification = await this.getRootNotification( |
|
74
|
|
|
leaveReaquest.getId() |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
const { id } = await this.mattermostNotifier.createComment( |
|
78
|
|
|
this.configService.get<string>('MATTERMOST_CHANNEL_LEAVES_ID'), |
|
79
|
|
|
message, |
|
80
|
|
|
rootNotification.getResourceId() |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
const notification = await this.notificationRepository.save( |
|
84
|
|
|
new Notification(type, message, id, leaveReaquest) |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
return notification.getId(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
throw new Error('Type not managed'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private async getRootNotification(leaveReaquestId: string) { |
|
94
|
|
|
return await this.notificationRepository.findByLeaveRequestIdAndType( |
|
95
|
|
|
leaveReaquestId, |
|
96
|
|
|
NotificationType.POST |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|